// subcontract/Mine/member/member.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    Templates: [],//会员配置
    AgnetTemplate: [],//权益
    activeIndex: 0,//选中的下标
    userinfo: getApp().userinfo,//用户信息
    pagePurchase: 1,//页码集采购
    pagingStatePurchase: false,//分页状态  集采购
    purchaseGoodsList: [],//集采购
  },
  /**集采购列表 */
  async getpdGoodsList() {
    let object = {
      page_no: this.data.pagePurchase,
      is_rand: 1
    }
    if (!this.data.pagingStatePurchase) {
      const { code, data } = await getApp().api.getpdGoodsList(object)
      if (code == 1) {
        this.setData({
          purchaseGoodsList: this.data.purchaseGoodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingStatePurchase: true
          })
          return
        }
        this.setData({
          pagePurchase: (this.data.pagePurchase * 1) + 1
        })
      }
    }
  },
  /**立即开通 */
  async rejcharge() {
    let object = {
      id: this.data.Templates[this.data.activeIndex].id
    }
    const { code, data } = await getApp().api.rejcharge(object)
    if (code == 1) {
      this.unifiedpay(data.id)
    }
  },
  /**调用支付 */
  async unifiedpay(order_id) {
    let object = {
      from: 'rejcharge',
      order_id,
      pay_way: '1'
    }
    const { code, data } = await getApp().api.unifiedpay(object)
    if (code == 1) {
      const ret = await getApp().requestPay(data)
      if (ret.errMsg == 'requestPayment:ok') {
        // navigateTo('/pages/Home/resultPay/resultPay?type=pay')
        console.log('支付成功');
        this.getUserinfo()
      } else {
        getApp().showToast('支付失败！')
      }
    }
  },
  async getUserinfo() {
    const { code, data } = await getApp().api.getUserinfo()
    if (code == 1) {
      getApp().local.set('userinfo', data)
      this.setData({
        userinfo: data
      })
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**集采购会员权益图和文字 */
  async getAgnetTemplate() {
    const { code, data } = await getApp().api.getAgnetTemplate()
    if (code == 1) {
      this.setData({
        AgnetTemplate: data
      })
    }
  },
  /**切换价格 */
  switchTab(event) {
    console.log(event);
    let { index } = event.currentTarget.dataset
    this.setData({
      activeIndex: index
    })
  },
  /**获取会员配置 */
  async getTemplates() {
    const { code, data } = await getApp().api.getTemplates()
    if (code == 1) {
      this.setData({
        Templates: data
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTemplates()
    this.getAgnetTemplate()
    this.getpdGoodsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getpdGoodsList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})